import type { ResponseData } from "@/types/common/response";
import type {
  AttendanceRecordQuery,
  AttendanceRecordVO,
  AttendanceRecordForm
} from "@/types/attendance/record";
import { http } from "@/utils/http";

/**
 * 查询考勤记录列表
 */
export function getAttendanceRecordList(query: AttendanceRecordQuery): Promise<
  ResponseData<{
    rows: AttendanceRecordVO[];
    total: number;
  }>
> {
  return http.request("get", "/attendance/record/list", { params: query });
}

/**
 * 获取考勤记录详情
 */
export function getAttendanceRecord(recordId: number): Promise<ResponseData<AttendanceRecordVO>> {
  return http.request("get", `/attendance/record/${recordId}`);
}

/**
 * 新增考勤记录
 */
export function addAttendanceRecord(data: AttendanceRecordForm): Promise<ResponseData<void>> {
  return http.request("post", "/attendance/record", { data });
}

/**
 * 修改考勤记录
 */
export function updateAttendanceRecord(data: AttendanceRecordForm): Promise<ResponseData<void>> {
  return http.request("put", "/attendance/record", { data });
}

/**
 * 删除考勤记录
 */
export function deleteAttendanceRecord(recordId: number): Promise<ResponseData<void>> {
  return http.request("delete", `/attendance/record/${recordId}`);
}

/**
 * 批量删除考勤记录
 */
export function batchDeleteRecords(recordIds: number[]): Promise<ResponseData<void>> {
  return http.request("delete", "/attendance/record/batch", { data: { recordIds } });
}

/**
 * 导出考勤记录
 */
export function exportAttendanceRecord(query: AttendanceRecordQuery): Promise<ResponseData<Blob>> {
  return http.request("get", "/attendance/record/export", {
    params: query,
    responseType: "blob"
  });
}

/**
 * 获取个人考勤统计
 */
export function getPersonalStats(
  personnelId: number,
  query: { beginDate: string; endDate: string }
): Promise<ResponseData<any>> {
  return http.request("get", `/attendance/record/stats/${personnelId}`, { params: query });
}

/**
 * 获取部门考勤统计
 */
export function getDepartmentStats(
  deptId: number,
  query: { beginDate: string; endDate: string }
): Promise<ResponseData<any>> {
  return http.request("get", `/attendance/record/stats/department/${deptId}`, { params: query });
}

/**
 * 考勤补卡申请
 */
export function applyRecordFix(data: {
  recordId: number;
  reason: string;
  fixTime: string;
}): Promise<ResponseData<void>> {
  return http.request("post", "/attendance/record/fix", { data });
}

/**
 * 审批考勤补卡
 */
export function approveRecordFix(
  fixId: number,
  status: string,
  remark?: string
): Promise<ResponseData<void>> {
  return http.request("put", `/attendance/record/fix/${fixId}`, {
    data: { status, remark }
  });
}

/**
 * 下载导入模板
 */
export function downloadImportTemplate(): Promise<ResponseData<Blob>> {
  return http.request("get", "/attendance/record/importTemplate", {
    responseType: "blob"
  });
}

/**
 * 导入考勤记录
 */
export function importAttendanceRecord(file: File): Promise<ResponseData<void>> {
  const formData = new FormData();
  formData.append("file", file);
  return http.request("post", "/attendance/record/import", {
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
}
